<meta charset="utf-8">
(#) TextView Internationalization

!!! WARNING: TextView Internationalization
   This is a warning.

Id
:   `SetTextI18n`
Summary
:   TextView Internationalization
Severity
:   Warning
Category
:   Internationalization
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   1.5.0 (November 2015)
Affects
:   Kotlin and Java files
Editing
:   This check runs on the fly in the IDE editor
See
:   https://developer.android.com/guide/topics/resources/localization.html
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/SetTextDetector.java)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/SetTextDetectorTest.kt)

When calling `TextView#setText`

* Never call `Number#toString()` to format numbers; it will not handle
  fraction separators and locale-specific digits properly. Consider
  using `String#format` with proper format specifications (`%d` or
  `%f`) instead.
* Do not pass a string literal (e.g. "Hello") to display text. Hardcoded
  text can not be properly translated to other languages. Consider
  using Android resource strings instead.
* Do not build messages by concatenating text chunks. Such messages can
  not be properly translated.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/CustomScreen.java:13:Warning: String literal in setText can
not be translated. Use Android resources instead. [SetTextI18n]
    view.setText("Hardcoded");
                 -----------
src/test/pkg/CustomScreen.java:17:Warning: Number formatting does not
take into account locale settings. Consider using String.format instead.
[SetTextI18n]
    view.setText(Integer.toString(50) + "%");
                 --------------------
src/test/pkg/CustomScreen.java:18:Warning: String literal in setText can
not be translated. Use Android resources instead. [SetTextI18n]
    view.setText(Double.toString(12.5) + " miles");
                                         --------
src/test/pkg/CustomScreen.java:21:Warning: String literal in setText can
not be translated. Use Android resources instead. [SetTextI18n]
    btn.setText("User " + getUserName());
                -------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is the source file referenced above:

`src/test/pkg/CustomScreen.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;

import android.content.Context;
import android.widget.Button;
import android.widget.TextView;

class CustomScreen {

  public CustomScreen(Context context) {
    TextView view = new TextView(context);

    // Should fail - hardcoded string
    view.setText("Hardcoded");
    // Should pass - no letters
    view.setText("-");
    // Should fail - concatenation and toString for numbers.
    view.setText(Integer.toString(50) + "%");
    view.setText(Double.toString(12.5) + " miles");

    Button btn = new Button(context);
    btn.setText("User " + getUserName());
    btn.setText(String.format("%s of %s users", Integer.toString(5), Integer.toString(10)));
    view.setText("");
  }

  private static String getUserName() {
    return "stub";
  }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/SetTextDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `SetTextDetector.test`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("SetTextI18n")
  fun method() {
     setText(...)
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("SetTextI18n")
  void method() {
     setText(...);
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection SetTextI18n
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="SetTextI18n" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'SetTextI18n'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore SetTextI18n ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>